#include "pieimagedisplay.h"

#include <QPainter>
#include <QVariantAnimation>
#include <QDebug>
#include <QtMath>
#include <QPainterPath>

#define ROTATE_ANGLE 360

PieImageWidget::PieImageWidget(QWidget *parent) : QWidget(parent)
{
    m_roateAnimation = new QVariantAnimation(this);
    m_roateAnimation->setDuration(1500);
    m_roateAnimation->setStartValue(0);
    m_roateAnimation->setEndValue(ROTATE_ANGLE);

    connect(m_roateAnimation, &QVariantAnimation::valueChanged,
            this, &PieImageWidget::animationValueChanged);
}

void PieImageWidget::setPixmap(const QPixmap &pixmap)
{
    if (m_pixmap != pixmap) {
        m_pixmap = pixmap;
        update();
    }
}

void PieImageWidget::setNextPixmap(int index)
{
    m_nextPixmap = m_lstImages[index];
}

QPixmap PieImageWidget::pixmap() const
{
    return m_pixmap;
}

void PieImageWidget::scalePixmap(const QPixmap& pixmap,int nMaxWidth,int nMaxHeight)
{
    m_pixmap = pixmap.scaled(nMaxWidth, nMaxHeight,
                             Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}

void PieImageWidget::initImageList(const QStringList &strImagePaths)
{
    m_lstImages.clear();

    foreach(QString str,strImagePaths)
        m_lstImages.append(QPixmap(str));
}

void PieImageWidget::start()
{
    if (m_roateAnimation->state() != QAbstractAnimation::Running) {
        isFinished = false;
        m_roateAnimation->start();
    }
}

void PieImageWidget::stop()
{
    if (m_roateAnimation->state() == QAbstractAnimation::Running) {
        m_roateAnimation->stop();
        isFinished = true;
    }
}

void PieImageWidget::pause()
{
    if (m_roateAnimation->state() == QAbstractAnimation::Running)
        m_roateAnimation->pause();
}

bool PieImageWidget::isRuning()
{
    return (m_roateAnimation->state() == QAbstractAnimation::Running);
}

void PieImageWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    if (m_pixmap.width() != width() || m_pixmap.height() != height())
        scalePixmap(m_pixmap, width(), height());

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    painter.setClipping(true);
    QPainterPath canDrawingPathArea;
    canDrawingPathArea.addRect(rect());
    int rectWidth = qSqrt(width()*width() + height()*height());
    QRect fillRect((rect().center().x() - rectWidth/2), (rect().center().y() - rectWidth/2), rectWidth, rectWidth);
    canDrawingPathArea.moveTo(rect().center());
    canDrawingPathArea.arcTo(fillRect, 0, m_dRoateAngle);
    painter.setBrush(QBrush(m_pixmap));
    painter.setPen(Qt::NoPen);
    painter.drawPixmap(rect(), m_nextPixmap);
    if (!isFinished)
        painter.drawPath(canDrawingPathArea);
}

void PieImageWidget::animationValueChanged(const QVariant &value)
{
    m_dRoateAngle = value.toInt();
    if (ROTATE_ANGLE == value.toInt()) {
        m_dRoateAngle = 0;
        isFinished = true;
        m_pixmap = m_nextPixmap;
        emit animationFinished();
    }

    update();
}
